import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { MenuItem, TreeNode, AutoComplete, Message } from 'primeng/primeng';
import { flyIn } from '../../common/animation/common.animation';
import { HumanService } from './service/human.service';

@Component({
  templateUrl: './human.component.html',
  styleUrls: ['./human.component.scss'],
  providers: [],
  animations: [flyIn]
})
export class HumanComponent implements OnInit {
  loading: boolean = true;
  unitHumans: TreeNode[];
  selectedNode: TreeNode;
  suggestions: any[];
  suggestion: any;
  constructor(
    private humanService: HumanService,
    private route: ActivatedRoute,
    private router: Router) { }

  ngOnInit() {
    this.humanService.getSimpleUnitHumanTree(0, true).subscribe(data => {
      this.loading = false;
      this.unitHumans = data;
      if (this.unitHumans[0]) {
        this.selectedNode = this.unitHumans[0];
        this.nodeSelect(null);
      }
    });

    //监听子组件中添加或删除事件，刷新树控件列表
    this.humanService.routeMsgPush$.subscribe(data => {
      if (!data.humanId) {
        for (var prop in data) {
          data = data[prop]
        }
      }
      this.recursionRefreshTreeNode(this.unitHumans, data);
    });
  }

  /** 树节点展开事件 */
  unitNodeExpand(event) {
    if (event.node && event.node.children.length == 0) {
      let unitId = +event.node.id;
      this.humanService.getSimpleUnitHumanTree(unitId, false).subscribe(nodes => event.node.children = nodes);
    }
  }
  /** 树节点选中事件 */
  nodeSelect(event) {
    if (this.selectedNode.data['type'] == 'U') {
      this.router.navigate(['system/human/list', { "id": this.selectedNode['id'] }]);
    } else {
      this.router.navigate(['system/human/info', { "id": this.selectedNode['id'] }]);
    }
  }
  /** 人员查找 */
  search(event) {
    if (!event.query) return;
    this.humanService.searchUnitHumanByName(event.query).subscribe(data => {
      this.suggestions = data;
    });
  }

  /**选择下拉事件 */
  selectSuggestion() {
    this.selectedNode = null;
    this.recursionExpendSuggestionTreeNode(this.unitHumans, this.suggestion.unitCode, this.suggestion.humanId);
  }

  /**递归展开查询的树 */
  recursionExpendSuggestionTreeNode(unitHumans: TreeNode[], humanUnitCode: string, humanId: string) {
    unitHumans.forEach(ele => {
      if (null != this.selectedNode) return;
      if (humanId == ele['id']) {
        this.selectedNode = ele;
        this.nodeSelect(null)
        return
      }
      if (humanUnitCode.indexOf(ele.data['code']) != -1) {
        //展开树节点
        ele.expanded = true;
        if (ele.children && ele.children.length > 0) {
          this.recursionExpendSuggestionTreeNode(ele.children, humanUnitCode, humanId);
        } else {
          this.humanService.getUnitHumanTree(+ele["id"], false).subscribe(nodes => {
            ele.children = nodes;
            this.recursionExpendSuggestionTreeNode(ele.children, humanUnitCode, humanId);
          });
        }
      } else {
        //关闭非节点
        ele.expanded = false;
      }
    })
  }

  /**递归处理树节点的变更信息 */
  recursionRefreshTreeNode(unitHumans: TreeNode[], human: any) {
    unitHumans.forEach(node => {
      if (node['id'] == human.unitId) {
        this.humanService.getSimpleUnitHumanTree(human.unitId, false).subscribe(nodes => node.children = nodes);
        return;
      }
      this.recursionRefreshTreeNode(node.children, human);
    });
  }
}
